<?php
/**
 * @title 1600. 皇位继承顺序
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class ThroneInheritance {
    /**
     * @since 2021-06-24 国王
     * @since 2021-06-24 关系数组
     * @since 2021-06-24 死亡的数组
     */
    private $kingName = '', $relationArray = [], $deathArray = [];

    /**
     * @param String $kingName
     */
    function __construct($kingName) {
        $this->kingName = $kingName;
    }

    /**
     * @param String $parentName
     * @param String $childName
     * @return NULL
     */
    function birth($parentName, $childName) {
        $this->relationArray[$parentName][] = $childName;
    }

    /**
     * @param String $name
     * @return NULL
     */
    function death($name) {
        $this->deathArray[$name] = 1;
    }

    /**
     * @return String[]
     */
    function getInheritanceOrder() {
        $result = [];
        self::curOrder($result, $this->kingName);

        /**
         * @return
         */
        return $result;
    }

    /**
     * @title 返回继承关系
     * @author start2004
     * @since 2021-06-24 Created
     *
     * @param array $result
     * @param string $name
     */
    function curOrder(&$result = [], $name = ''){
        /**
         * @since 2021-06-24 判断是否活着, 决定是否加入继承队列
         */
        if(!isset($this->deathArray[$name])){
            $result[] = $name;
        } else {}

        /**
         * @since 2021-06-24 迭代自己子女
         */
        if(isset($this->relationArray[$name])){
            foreach ($this->relationArray[$name] as $childName){
                self::curOrder($result, $childName);
            }
        } else {}
    }
}

/**
 * @url http://127.0.0.1/leetcode/202106/2021.06.20.php
 */
$datas = [
    [
        ["ThroneInheritance", "birth", "birth", "birth", "birth", "birth", "birth", "getInheritanceOrder", "death", "getInheritanceOrder"],
        [["king"], ["king", "andy"], ["king", "bob"], ["king", "catherine"], ["andy", "matthew"], ["bob", "alex"], ["bob", "asha"], [null], ["bob"], [null]],
    ],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
// foreach ($datas as $data){
//     var_dump($data);
//
//     $obj = new Solution();
//     $result = $obj->(...$data);
//     // $result = $obj->($xhprof->tree($data));
//     // $result = $obj->($xhprof->listNode($data));
//     // $result = $obj->find(...$data);
//     var_dump($result);
//     // if(count($result)<=20){
//     //     var_dump($result);
//     // } else {
//     //     var_dump(count($result));
//     // }
//     echo str_repeat(PHP_EOL, 3);
//     echo str_repeat("<br>", 3);
// }

// $obj = new ThroneInheritance("king");
// $obj->birth("king", "andy");
//
// die();


foreach ($datas as $data){
    $obj = new $data[0][0](...$data[1][0]);

    for ($i=1; $i<count($data[0]); $i++){
        $func = $data[0][$i];
        $param = $data[1][$i];
        $result = $obj->$func(...$param);

        echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
            if($result === true){
                echo "True";
            } elseif($result === false) {
                echo "False";
            } elseif($result === null) {
                echo "Null";
            } elseif(is_array($result)) {
                var_dump($result);
            } else {
                echo $result;
            }
        echo PHP_EOL;
    }

    echo str_repeat(PHP_EOL, 3);
}
$xhprof->end();
